package huxibianjie.com.lbscandroid.ui.old;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.CountDailyModel;
import huxibianjie.com.lbscandroid.model.RankingsModel;
import huxibianjie.com.lbscandroid.pedomemter.CustomBarChart;
import huxibianjie.com.lbscandroid.task.Callback;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;

public class RankingsActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout customBarChart1;
    private int[] chartData;
    private String[] xLabel;
    private String[] yLabel;
    private ImageView mIsClick;
    private RecyclerView recycle;
    private NearbyRankingsAdapter nearby_rankings_adapter;
    private TextView getupText;


    @Override
    protected int getContentView() {
        return R.layout.activity_rankings;
    }

    @Override
    protected void initView() {
        customBarChart1 = (LinearLayout) findViewById(R.id.customBarChart1);
        recycle = (RecyclerView) findViewById(R.id.recycle_rankings);
        getupText = (TextView) findViewById(R.id.get_up_text);
        recycle.setLayoutManager(new LinearLayoutManager(this));
        nearby_rankings_adapter = new NearbyRankingsAdapter();
        recycle.setAdapter(nearby_rankings_adapter);
        mIsClick = (ImageView) findViewById(R.id.is_click);
        mIsClick.setOnClickListener(this);
        String ids = String.valueOf(UserManager.getInstance().getUserModel().getId());
        Task.create(Service.class).getTodayRankings(ids).enqueue(new Callback<RankingsModel>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(@Nullable RankingsModel rankingsModel) {
                assert rankingsModel != null;
                int myFlag = rankingsModel.getRankingFlag();
                int stepNumber = rankingsModel.getStepNumber();
                if (myFlag == 0) {
                    getupText.setText("今日未上榜,距离上一名还差" + stepNumber + "LBSC");
                } else {
                    getupText.setText("今日已上榜,距离上一名还差" + stepNumber + "LBSC");
                }
                nearby_rankings_adapter.setDatas(rankingsModel.getRankings());
            }

            @Override
            public void onFailure(@NonNull String error) {
                showToast("获取失败");
            }

        });

        Task.create(Service.class).getHistoryData(ids).enqueue(new Callback<ArrayList<CountDailyModel>>() {

            @Override
            public void onSuccess(@Nullable ArrayList<CountDailyModel> arrayList) {
                int day = 7;
                if (arrayList != null) {
                    xLabel = new String[day + 1];
                    for (int i = 0; i < xLabel.length; i++) {
                        xLabel[i] = "";
                    }
                    chartData = new int[day + 1];
                    Collections.reverse(arrayList);
                    while (arrayList.size() > day) {
                        arrayList.remove(0);
                    }
                    for (int i = 0; i < arrayList.size(); i++) {
                        chartData[i + 1] = arrayList.get(i).getNumber();
                        xLabel[i + 1] = arrayList.get(i).getTime().substring(5);
                    }
                    initChart();
                } else {
                    showToast("获取失败");
                }
            }

            @Override
            public void onFailure(@NonNull String error) {
                showToast(error);
                xLabel = new String[]{"", "", "", "", "", "", "", ""};
                chartData = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
                initChart();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }


    /**
     * 初始化柱状图1数据
     */
    private void initChart() {
        yLabel = new String[]{"0", "2000", "4000", "6000", "8000", "10000", "12000", "14000", "16000", "18000"};
        List<int[]> data = new ArrayList<>();
        data.add(chartData);

        List<Integer> color = new ArrayList<>();
        color.add(R.color.white);
        color.add(R.color.white);
        color.add(R.color.white);
        customBarChart1.addView(new CustomBarChart(this, xLabel, yLabel, data, color));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.is_click:
                finish();
                break;
        }
    }
}
